<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Activity extends CI_Controller
{
	private $user_id;
	function __construct()
	{
		parent::__construct();
		
		$this->load->config('layout', TRUE);
		$this->load->helper('url');
		$this->load->library('security');
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('tank_auth');
		$this->load->library('googleapi');
		$this->lang->load('tank_auth');
		$this->lang->load('activity');
		$this->load->model('activity_model');
		$this->user_id = $this->session->userdata('user_id');
		
	}

	function index($activity_id)
	{
		$activity = array();
		$data = $this->session->all_userdata();
		$data['user'] = $this->users->get_user_by_activity_id($activity_id,1);
		
		$this->load->view('coplay/header', $data);
		$activity = $this->activity_model->get_by_id($activity_id,$this->user_id);
		$data['user']['join_count'] = $this->activity_model->get_join_count($activity_id);
		$data['user']['activities'] = $this->activity_model->get($data['user']['id']);
			 
		$data['upload_image'] = json_decode($data['user']['upload_image']);
		$data['user']['image_show'] = $this->load->view('coplay/activity/image_show', $data, true);
		$this->load->view('coplay/activity/index',$data['user']);
	}
	
	function get_blog_result(){
		$post_data = $this->input->post(NULL, TRUE); // returns all POST items with XSS filter
		$this->load->library('googleapi');
		$json = $this->googleapi->getBlogResults($post_data['start'],$post_data['q']);
		
        $next_start = '';
        $previous_start = '';
        $currentPageIndex = @$json->responseData->cursor->currentPageIndex;
        
        if (isset($json->responseData->cursor->pages[$currentPageIndex + 1])) {
            $next_start = @$json->responseData->cursor->pages[$currentPageIndex + 1]->start;
        
        }
        
        if (isset($json->responseData->cursor->pages[$currentPageIndex - 1])) {
            $previous_start = @$json->responseData->cursor->pages[$currentPageIndex - 1]->start;
        }
        
        
        $results = @$json->responseData->results;
        $blog_results = '';
		
		
        foreach (@$results as $key=>$data) {
        	$blog_results .= '<div class="blog_result_block">';
            $blog_results .= '<h2 class="blog_result_title"><a href="'.$data->postUrl.'" target="_blank">'.$data->title.'</a></h2>';
            $blog_results .= '<p>'.strip_tags($data->content).'</p>';
			$blog_results .= '</div>';
        }
       
		if ($next_start != '') {	
			$blog_results .= '<div id="get_more" class="blog_result_block"><a href="javascript:;" onclick="getBlogResult(this,\''.$next_start.'\',\''.$post_data['q'].'\')" id="next">more</a></div>';
		}
		else{
			$blog_results .= '';
		}
		
		//echo json_encode(array('results'=>$blog_results,'get_more'=>$get_more));
		echo $blog_results;
	}	
}